import getType from './getType';
import dayjs from 'dayjs';

interface DateFormateOption {
	format?: string;
}

const dateFormate = function (date: number | string | Date, option: DateFormateOption = {}): string | undefined {
	let type = getType(date);
	const { format } = option;
	if (type === 'Number' && typeof date === 'number' && date <= 365 && date >= -365) {
		return dayjs()
			.add(date, 'day')
			.format(format || 'YYYY-MM-DD');
	}
	if (type === 'Number') {
		return dayjs(date).format(format || 'YYYY-MM-DD HH:mm:ss');
	}
	if (type === 'String') {
		return dayjs(date).format(format || 'YYYY-MM-DD HH:mm:ss');
	}
	if (type === 'Date') {
		return dayjs(date).format(format || 'YYYY-MM-DD HH:mm:ss');
	}
};

export default dateFormate;
